package com.hotel.front;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.labs.repackaged.org.json.JSONException;
import com.google.gson.Gson;
import com.hotel.BL.GenericLogic;
import com.hotel.BL.LanguageLogic;

import data.General;
import data.LanguageMaster;

@SuppressWarnings("serial")
public class LanguageService extends HttpServlet{
	// http://localhost:8080/languageService?func
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		try {
			String doClass = req.getParameter("func");
			if (doClass.equals("editUsingLanguage")) {
				EditUsingLanguage(req, resp);
			}else if(doClass.equals("getLanguage4Admin")){
				GetLanguage4Admin(req,resp);
			}else if(doClass.equals("delLanguage")){
				DeleteLanguage(req,resp);
			}else if(doClass.equals("getLanguageForDropDown")){
				GetLanguageForDropDown(req,resp);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void EditUsingLanguage(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{		
		HttpSession session=req.getSession(true);	
		long hotelId = (Long) session.getAttribute("hotelId");
		String id = req.getParameter("id");
		GenericLogic gLogic = new GenericLogic(req,resp);
		General g = gLogic.GetGeneral(hotelId);
		g.setUsingLanguageString(id);
		
		gLogic.UpdateGenerlic(g);

		GetLanguage4Admin(req, resp);
	}

	private void DeleteLanguage(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		HttpSession session=req.getSession(true);	
		long hotelId = (Long) session.getAttribute("hotelId");
		String id = req.getParameter("id");
		
		new LanguageLogic(req,resp).DeleteLanguage(hotelId,id);
		
		GetLanguage4Admin(req, resp);
	}
	
	private void GetLanguage4Admin(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		HttpSession session=req.getSession(true);	
		long hotelId = (Long) session.getAttribute("hotelId");
		long offset = Long.parseLong(req.getParameter("offset"));
		double line = Double.parseDouble(req.getParameter("line"));
		long pointer = Long.parseLong(req.getParameter("pagePointer"));
		
		List<LanguageMaster> obj = new LanguageLogic(req,resp).GetAllLanguage4AdminLimit(hotelId,offset,(long)line);
		long dataSize = new GenericLogic(req,resp).GetGeneral(hotelId).getUsingLanguage().size();
		String defaultLanguage = new GenericLogic(req,resp).GetGeneral(hotelId).getDefaultLanguageString();
		
		Gson gson = new Gson();
		
		HashMap<String,Object> data = new HashMap<String,Object>();
		data.put("dataAmount",dataSize);
		data.put("pageNumber",pointer);
		data.put("lineAmount", line);
		data.put("pageAmount", Math.ceil(dataSize/line));
		data.put("data", obj);
		data.put("defaultLanguage", defaultLanguage);
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(data));
	}
	
	private void GetLanguageForDropDown(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{		
		HttpSession session=req.getSession(true);	
		long hotelId = (Long) session.getAttribute("hotelId");
		List<LanguageMaster> usingLanguage = new GenericLogic(req,resp).GetGeneral(hotelId).getUsingLanguage();
		List<LanguageMaster> allLanguage = new LanguageLogic(req,resp).GetLanguageRemain(usingLanguage);
		String defaultLanguage = new GenericLogic(req,resp).GetGeneral(hotelId).getDefaultLanguageString();
		Gson gson = new Gson();
		HashMap<String,Object> data = new HashMap<String,Object>();
		data.put("allLanguage",allLanguage);
		data.put("usingLanguage",usingLanguage);
		data.put("defaultLanguage",defaultLanguage);
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(data));
	}

}
